<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>用swoole TCP Server 和mongodb做数据备份和恢复 | Webwlsong`s Blog</title>
  <meta name="description" content="应用场景是，单个用户写的数据是8-10条/S JSON数据包 采用架构如下图所示：  采用的是  php7.0.6 swoole-1.8.5版本 php扩展mongodb1.1.6  而目前mongo-1.6.14不支持php7 这样导致连接Mongo函数不一样，后面会讲到。 服务器 Ubuntu 14.04 64位 2核 4G 阿里云的Mongodb服务器  到目前阿里云Mongodb还不支持s">
<meta name="keywords" content="mongo,php-mongo,swoole,tcp">
<meta property="og:type" content="article">
<meta property="og:title" content="用swoole TCP Server 和mongodb做数据备份和恢复">
<meta property="og:url" content="https://webwlsong.gitee.io/2016/05/18/mongo-php-error/index.html">
<meta property="og:site_name" content="webwlsong">
<meta property="og:description" content="应用场景是，单个用户写的数据是8-10条/S JSON数据包 采用架构如下图所示：  采用的是  php7.0.6 swoole-1.8.5版本 php扩展mongodb1.1.6  而目前mongo-1.6.14不支持php7 这样导致连接Mongo函数不一样，后面会讲到。 服务器 Ubuntu 14.04 64位 2核 4G 阿里云的Mongodb服务器  到目前阿里云Mongodb还不支持s">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://webwlsong.gitee.io/2016/05/18/mongo-php-error/Swoole-Mongo.png">
<meta property="og:updated_time" content="2020-09-23T16:35:34.805Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="用swoole TCP Server 和mongodb做数据备份和恢复">
<meta name="twitter:description" content="应用场景是，单个用户写的数据是8-10条/S JSON数据包 采用架构如下图所示：  采用的是  php7.0.6 swoole-1.8.5版本 php扩展mongodb1.1.6  而目前mongo-1.6.14不支持php7 这样导致连接Mongo函数不一样，后面会讲到。 服务器 Ubuntu 14.04 64位 2核 4G 阿里云的Mongodb服务器  到目前阿里云Mongodb还不支持s">
<meta name="twitter:image" content="https://webwlsong.gitee.io/2016/05/18/mongo-php-error/Swoole-Mongo.png">
  <!-- Canonical links -->
  <link rel="canonical" href="https://webwlsong.gitee.io/2016/05/18/mongo-php-error/index.html">
  
    <link rel="alternate" href="/atom.xml" title="webwlsong" type="application/atom+xml">
  
  
    <link rel="icon" href="https://webwlsong-1301722012.cos.ap-shanghai.myqcloud.com/mweb/2020/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/css/style.css">
  
  
  
  
</head>


<body class="main-center theme-black" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/webwlsong" target="_blank">
          <img class="img-circle img-rotate" src="https://static01.imgkr.com/temp/e857da9acde846d4a09582984209f398.png" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">webwlsong</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Developer</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> ShangHai, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav ">
        
        
        <li class="menu-item menu-item-home">
          <a href="/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-books">
          <a href="/books">
            
            <i class="icon icon-book-fill"></i>
            
            <span class="menu-title">书单</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/links">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">友链</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/webwlsong" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://weibo.com/webwlsong" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="https://twitter.com/webwlsong" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
        
        <li><a href="https://www.behance.net/webwlsong" target="_blank" title="Behance" data-toggle=tooltip data-placement=top><i class="icon icon-behance"></i></a></li>
        
        <li><a href="/atom.xml" target="_blank" title="Rss" data-toggle=tooltip data-placement=top><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/go/">go</a><span class="category-list-count">2</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/go/golang/">golang</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/go/内存/">内存</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/go/内存/堆栈/">堆栈</a><span class="category-list-count">1</span></li></ul></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/mono/">mono</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nginx/">nginx</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/python/">python</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/小程序/">小程序</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签</h3>
    <div class="widget-body">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/c/">c</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/c/">c++</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/code/">code</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/delve/">delve</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fira/">fira</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/frp/">frp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gitlab/">gitlab</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/golang/">golang</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo/">hexo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/jetbrains/">jetbrains</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongo/">mongo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mono/">mono</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/operator/">operator</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php/">php</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php-jit/">php-jit</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/php-mongo/">php-mongo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/phpstrom/">phpstrom</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pure/">pure</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/pyenv/">pyenv</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/">python</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/swoole/">swoole</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tcp/">tcp</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/ubuntu/">ubuntu</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/内存/">内存</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/内网穿透/">内网穿透</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/堆栈/">堆栈</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/小程序/">小程序</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/性能测试/">性能测试</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/架构/">架构</a><span class="tag-list-count">1</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/tags/c/" style="font-size: 13px;">c</a> <a href="/tags/c/" style="font-size: 13px;">c++</a> <a href="/tags/code/" style="font-size: 13px;">code</a> <a href="/tags/delve/" style="font-size: 13px;">delve</a> <a href="/tags/docker/" style="font-size: 13.5px;">docker</a> <a href="/tags/fira/" style="font-size: 13px;">fira</a> <a href="/tags/frp/" style="font-size: 13px;">frp</a> <a href="/tags/gitlab/" style="font-size: 13px;">gitlab</a> <a href="/tags/go/" style="font-size: 14px;">go</a> <a href="/tags/golang/" style="font-size: 13px;">golang</a> <a href="/tags/hexo/" style="font-size: 13px;">hexo</a> <a href="/tags/jetbrains/" style="font-size: 13px;">jetbrains</a> <a href="/tags/linux/" style="font-size: 13.5px;">linux</a> <a href="/tags/mongo/" style="font-size: 13px;">mongo</a> <a href="/tags/mono/" style="font-size: 13px;">mono</a> <a href="/tags/nginx/" style="font-size: 14px;">nginx</a> <a href="/tags/nodejs/" style="font-size: 13px;">nodejs</a> <a href="/tags/operator/" style="font-size: 13px;">operator</a> <a href="/tags/php/" style="font-size: 13.5px;">php</a> <a href="/tags/php-jit/" style="font-size: 13px;">php-jit</a> <a href="/tags/php-mongo/" style="font-size: 13px;">php-mongo</a> <a href="/tags/phpstrom/" style="font-size: 13px;">phpstrom</a> <a href="/tags/pure/" style="font-size: 13px;">pure</a> <a href="/tags/pyenv/" style="font-size: 13px;">pyenv</a> <a href="/tags/python/" style="font-size: 13px;">python</a> <a href="/tags/swoole/" style="font-size: 13px;">swoole</a> <a href="/tags/tcp/" style="font-size: 13px;">tcp</a> <a href="/tags/ubuntu/" style="font-size: 14px;">ubuntu</a> <a href="/tags/内存/" style="font-size: 13px;">内存</a> <a href="/tags/内网穿透/" style="font-size: 13px;">内网穿透</a> <a href="/tags/堆栈/" style="font-size: 13px;">堆栈</a> <a href="/tags/小程序/" style="font-size: 13px;">小程序</a> <a href="/tags/性能测试/" style="font-size: 13px;">性能测试</a> <a href="/tags/架构/" style="font-size: 13px;">架构</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/09/">九月 2020</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">八月 2020</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/05/">五月 2016</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/04/">四月 2016</a><span class="archive-list-count">4</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/小程序/">小程序</a>
              </p>
              <p class="item-title">
                <a href="/2020/09/23/2020/2020-09/2020-09-24/" class="title">图解微信第三方开放平台流程图</a>
              </p>
              <p class="item-date">
                <time datetime="2020-09-23T16:00:00.000Z" itemprop="datePublished">2020-09-24</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/python/">python</a>
              </p>
              <p class="item-title">
                <a href="/2020/09/17/2020/2020-09/2020-09-18/" class="title">pyenv的安装和简单使用（git、pyenv、pyenv-virtualenv）</a>
              </p>
              <p class="item-date">
                <time datetime="2020-09-17T16:00:00.000Z" itemprop="datePublished">2020-09-18</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/categories/go/">go</a><i class="icon icon-angle-right"></i><a class="category-link" href="/categories/go/内存/">内存</a>
              </p>
              <p class="item-title">
                <a href="/2020/09/02/2020/2020-09/2020-09-02/" class="title">图解Golang的内存管理分配</a>
              </p>
              <p class="item-date">
                <time datetime="2020-09-02T16:00:00.000Z" itemprop="datePublished">2020-09-03</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/2020/08/31/2020/2020-08/2020-08-31/" class="title">DIY服务器搭建，内网穿透搭建</a>
              </p>
              <p class="item-date">
                <time datetime="2020-08-31T09:09:00.000Z" itemprop="datePublished">2020-08-31</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/2020/08/30/2020/2020-08/2020-08-30-1/" class="title">Ubuntu20.04.1关闭图形界面，使用tty登陆</a>
              </p>
              <p class="item-date">
                <time datetime="2020-08-30T03:08:00.000Z" itemprop="datePublished">2020-08-30</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-mongo-php-error" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      用swoole TCP Server 和mongodb做数据备份和恢复
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/2016/05/18/mongo-php-error/" class="article-date">
	  <time datetime="2016-05-18T10:52:40.000Z" itemprop="datePublished">2016-05-18</time>
	</a>
</span>
        
        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/tags/mongo/">mongo</a>, <a class="article-tag-link" href="/tags/php-mongo/">php-mongo</a>, <a class="article-tag-link" href="/tags/swoole/">swoole</a>, <a class="article-tag-link" href="/tags/tcp/">tcp</a>
  </span>


        

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/2016/05/18/mongo-php-error/#comments" class="article-comment-link">评论</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <p>应用场景是，单个用户写的数据是8-10条/S JSON数据包</p>
<p>采用架构如下图所示：<br>
<img src="Swoole-Mongo.png" alt="架构图"></p>
<p>采用的是</p>
<ul>
<li>php7.0.6</li>
<li>swoole-1.8.5版本</li>
<li>php扩展mongodb1.1.6  而目前mongo-1.6.14不支持php7 这样导致连接Mongo函数不一样，后面会讲到。</li>
<li>服务器 Ubuntu 14.04 64位 2核 4G</li>
<li>阿里云的Mongodb服务器</li>
</ul>
<p>到目前阿里云Mongodb还不支持sharding,后续会进行支持。目前是使用双节点。摘录<a href="https://yq.aliyun.com/articles/7557" target="_blank" rel="noopener">MongoDB杭州用户交流会</a>的一段话:</p>
<blockquote>
<p>阿里云目前已提供对MongoDB复制集（Replica Set）的支持，默认会为用户创建包含3个数据节点的复制集，其中一个Primary、一个Secondary，以及一个Hidden节点。Primary、Secondary对用户可见，用户可以自定义ReadPreference，Hidden节点对用户不可见，目前主要用于实例数据备份以及自动的failover，当有Primary或Secondary节点挂掉时，Hidden会被自动切换为Secondary，保证用户的服务不受影响。</p>
</blockquote>
<h3 id="mongodb服务器"><a class="markdownIt-Anchor" href="#mongodb服务器"></a> mongodb服务器</h3>
<p>阿里云服务器目前只支持ECS访问不支持外网方面</p>
<ul>
<li>
<p>[公网连接mongodb windows篇](<a href="https://help.aliyun.com/knowledge_detail/13052608.html#%E9%80%9A%E8%BF%87%E5%85%AC%E7%BD%91%E8%BF%9E%E6%8E%A5%E4%BA%91%E6%95%B0%E6%8D%AE%E5%BA%93MongoDB--ECS" target="_blank" rel="noopener">https://help.aliyun.com/knowledge_detail/13052608.html#通过公网连接云数据库MongoDB--ECS</a> Windows篇)</p>
</li>
<li>
<p>[公网连接mongodb linux篇](<a href="https://help.aliyun.com/knowledge_detail/13052572.html#%E9%80%9A%E8%BF%87%E5%85%AC%E7%BD%91%E8%BF%9E%E6%8E%A5%E4%BA%91%E6%95%B0%E6%8D%AE%E5%BA%93MongoDB--ECS" target="_blank" rel="noopener">https://help.aliyun.com/knowledge_detail/13052572.html#通过公网连接云数据库MongoDB--ECS</a> Linux篇)</p>
</li>
</ul>
<p>用如下命令检测是否连上mongodb</p>
<ul>
<li>ping  <a href="http://dds-xxxxxxxx.mongodb.rds.aliyuncs.com" target="_blank" rel="noopener">dds-xxxxxxxx.mongodb.rds.aliyuncs.com</a></li>
<li>telnet  <a href="http://dds-xxxxxxxxxxxx.mongodb.rds.aliyuncs.com" target="_blank" rel="noopener">dds-xxxxxxxxxxxx.mongodb.rds.aliyuncs.com</a> 3717</li>
<li>mongo --host <a href="http://dds-xxxxxxxxxxxxx1.mongodb.rds.aliyuncs.com:3717" target="_blank" rel="noopener">dds-xxxxxxxxxxxxx1.mongodb.rds.aliyuncs.com:3717</a> --authenticationDatabase admin -u root -p</li>
<li>mongo --host <a href="http://dds-xxxxxxxxxxxxxx2.mongodb.rds.aliyuncs.com:3717" target="_blank" rel="noopener">dds-xxxxxxxxxxxxxx2.mongodb.rds.aliyuncs.com:3717</a> --authenticationDatabase admin -u root -p</li>
</ul>
<p>如果本身server服务器用的ECS，还是要设置一下，</p>
<h3 id="搭建tcp-sever服务器"><a class="markdownIt-Anchor" href="#搭建tcp-sever服务器"></a> 搭建TCP-SEVER服务器</h3>
<ol>
<li>买好服务器之后习惯性的把服务器服务升级到最新。（这个是强迫症）</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ apt-get update </span><br><span class="line"></span><br><span class="line">$ apt-get upgrade</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>安装最新的php7.0.6环境</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">apt<span class="literal">-get</span> install python<span class="literal">-software</span><span class="literal">-properties</span></span><br><span class="line"></span><br><span class="line">apt<span class="literal">-get</span> install software<span class="literal">-properties</span><span class="literal">-common</span></span><br><span class="line"></span><br><span class="line">LC_ALL=C.UTF<span class="literal">-8</span> <span class="built_in">add-apt</span><span class="literal">-repository</span> ppa:ondrej/php</span><br><span class="line"></span><br><span class="line">apt<span class="literal">-get</span> update</span><br><span class="line"></span><br><span class="line">apt<span class="literal">-cache</span> show php7.<span class="number">0</span><span class="literal">-dev</span>  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Package: php7.<span class="number">0</span><span class="literal">-dev</span></span><br><span class="line">Source: php7.<span class="number">0</span></span><br><span class="line">Priority: optional</span><br><span class="line">Section: php</span><br><span class="line">Installed<span class="literal">-Size</span>: <span class="number">4658</span></span><br><span class="line">Maintainer: Debian PHP Maintainers &lt;pkg<span class="literal">-php</span><span class="literal">-maint</span>@lists.alioth.debian.org&gt;</span><br><span class="line">Architecture: amd64</span><br><span class="line">Version: <span class="number">7.0</span>.<span class="number">6</span><span class="literal">-12</span>+donate.sury.org~trusty+<span class="number">1</span></span><br><span class="line">Recommends: dh<span class="literal">-php</span>, pkg<span class="literal">-php</span><span class="literal">-tools</span></span><br><span class="line">Depends: autoconf (&gt;= <span class="number">2.63</span>), automake (&gt;= <span class="number">1.11</span>), libpcre3<span class="literal">-dev</span>, libssl<span class="literal">-dev</span>, php7.<span class="number">0</span><span class="literal">-cli</span> (&gt;= <span class="number">7.0</span>.<span class="number">6</span><span class="literal">-12</span>+donate.sury.org~trusty+<span class="number">1</span>), php7.<span class="number">0</span><span class="literal">-common</span> (= <span class="number">7.0</span>.<span class="number">6</span><span class="literal">-12</span>+donate.sury.org~trusty+<span class="number">1</span>), shtool, libtool</span><br><span class="line">Filename: pool/main/p/php7.<span class="number">0</span>/php7.<span class="number">0</span><span class="literal">-dev_7</span>.<span class="number">0.6</span><span class="literal">-12</span>+donate.sury.org~trusty+<span class="number">1</span>_amd64.deb</span><br><span class="line">Size: <span class="number">505784</span></span><br><span class="line">MD5sum: bb75fadf2fc0bae22d7a26999b9ac0e2</span><br><span class="line">SHA1: a85e13a259faae1377813d7b1e5c344c8ede8d09</span><br><span class="line">SHA256: <span class="number">79617</span>d3b79e054129a2e980534885ab794ec18c5052894e71d1da37ec07b2aff</span><br><span class="line">Description<span class="literal">-en</span>: Files <span class="keyword">for</span> PHP7.<span class="number">0</span> module development</span><br><span class="line"> This package provides the files from the PHP7.<span class="number">0</span> source needed <span class="keyword">for</span> compiling</span><br><span class="line"> additional modules.</span><br><span class="line"></span><br><span class="line">PHP (recursive acronym <span class="keyword">for</span> PHP: Hypertext Preprocessor) is a widely<span class="literal">-used</span></span><br><span class="line"> open source general<span class="literal">-purpose</span> scripting language that is especially suited</span><br><span class="line"> <span class="keyword">for</span> web development and can be embedded into HTML.</span><br><span class="line">Description<span class="literal">-md5</span>: cab4eaaf141b1f52bc2814eea2492ab2</span><br><span class="line"></span><br><span class="line">apt<span class="literal">-get</span> install php7.<span class="number">0</span><span class="literal">-dev</span></span><br></pre></td></tr></table></figure>
<ol start="3">
<li>安装mongo扩展 和swoole扩展</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">使用pecl进行安装mongo和swoole扩展。</span><br><span class="line">这里记住是mongodb扩展不是mongo扩展。这两个扩展在使用上面不一样，后面会讲到。这里我们安装</span><br><span class="line">mongodb</span><br><span class="line"></span><br><span class="line">搜索mongodb</span><br><span class="line">$ pecl search mongo</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pecl库更新到最新</span><br><span class="line">$ pecl channel-update pecl.php.net</span><br><span class="line"></span><br><span class="line">安装mongodb</span><br><span class="line">$ pecl install mongodb</span><br><span class="line"></span><br><span class="line">问题：</span><br><span class="line">1. 这里如果安装mongo会提示不支持Php7.0以上版本：</span><br><span class="line">	pecl/mongo requires PHP (version &gt;= 5.3.0, version &lt;= 5.99.99),</span><br><span class="line">	installed version is 7.0.6-12+donate.sury.org~trusty+1</span><br><span class="line">2. configure: error: Cannot find OpenSSL&apos;s libraries</span><br><span class="line">	 解决方法：  ln -s /usr/lib/x86_64-linux-gnu/libssl.so  /usr/lib</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">安装swoole</span><br><span class="line">$ pecl install swoole</span><br><span class="line"></span><br><span class="line">查找php.ini 把swoole.so和mongodb加入进去</span><br><span class="line">$ php --ini  </span><br><span class="line"></span><br><span class="line">$ php -ri|grep swoole</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>PHP swoole代码</li>
</ol>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">$serv = <span class="keyword">new</span> swoole_server(<span class="string">"0.0.0.0"</span>, <span class="number">9501</span>);</span><br><span class="line"></span><br><span class="line">$serv-&gt;set(<span class="keyword">array</span>(</span><br><span class="line">	<span class="string">'worker_num'</span> =&gt; <span class="number">8</span>, <span class="comment">//工作进程数量</span></span><br><span class="line">	<span class="string">'daemonize'</span> =&gt; <span class="keyword">false</span>, <span class="comment">//是否作为守护进程</span></span><br><span class="line">));</span><br><span class="line"></span><br><span class="line">$serv-&gt;on(<span class="string">'connect'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv, $fd)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">echo</span> <span class="string">"Client:Connect.\n"</span>;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$serv-&gt;on(<span class="string">'receive'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv,$fd, $from_id, $data)</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	$arr = json_decode($data);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//$array=array('column_name'=&gt;'col'.rand(100,999),'column_exp'=&gt;'xiaocai');</span></span><br><span class="line">	$connection = <span class="keyword">new</span> MongoClient(<span class="string">"mongodb://root:密码@主机ID1.mongodb.rds.aliyuncs.com:3717,主机2.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=副本集名称"</span>);</span><br><span class="line">	<span class="comment">//	$connection = new MongoClient("mongodb://localhost:27017");</span></span><br><span class="line">	var_dump($connection);</span><br><span class="line">	$roomid = <span class="string">"roomid"</span>.<span class="string">'_123'</span>;</span><br><span class="line">	$collection = $connection-&gt;test-&gt;$roomid;</span><br><span class="line">	$collection-&gt;insert($arr);</span><br><span class="line">	</span><br><span class="line">	$document = $collection-&gt;findOne();</span><br><span class="line">	var_dump($document);</span><br><span class="line">	</span><br><span class="line">	$serv-&gt;send($fd, <span class="string">'Swoole: '</span> . $data);</span><br><span class="line">	$serv-&gt;close($fd);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$serv-&gt;on(<span class="string">'close'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv, $fd)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">echo</span> <span class="string">"Client: Close.\n"</span>;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$serv-&gt;start();</span><br></pre></td></tr></table></figure>
<p>如果执行上面的代码的话，会报<code>PHP Fatal error: Uncaught Error: Class 'MongoClient' not found</code><br>
这里要说明一下MongoClient是扩展Mongo的内置函数。<br>
可以通过</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print_r(get_declared_classes());</span><br></pre></td></tr></table></figure>
<p>查看函数列表<br>
Mongo的函数列表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">[156] =&gt; MongoClient</span><br><span class="line">[157] =&gt; Mongo</span><br><span class="line">[158] =&gt; MongoDB</span><br><span class="line">[159] =&gt; MongoCollection</span><br><span class="line">[160] =&gt; MongoCursor</span><br><span class="line">[161] =&gt; MongoCommandCursor</span><br><span class="line">[162] =&gt; MongoGridFS</span><br><span class="line">[163] =&gt; MongoGridFSFile</span><br><span class="line">[164] =&gt; MongoGridFSCursor</span><br><span class="line">[165] =&gt; MongoWriteBatch</span><br><span class="line">[166] =&gt; MongoInsertBatch</span><br><span class="line">[167] =&gt; MongoUpdateBatch</span><br><span class="line">[168] =&gt; MongoDeleteBatch</span><br><span class="line">[169] =&gt; MongoId</span><br><span class="line">[170] =&gt; MongoCode</span><br><span class="line">[171] =&gt; MongoRegex</span><br><span class="line">[172] =&gt; MongoDate</span><br><span class="line">[173] =&gt; MongoBinData</span><br><span class="line">[174] =&gt; MongoDBRef</span><br><span class="line">[175] =&gt; MongoException</span><br><span class="line">[176] =&gt; MongoConnectionException</span><br><span class="line">[177] =&gt; MongoCursorException</span><br><span class="line">[178] =&gt; MongoCursorTimeoutException</span><br><span class="line">[179] =&gt; MongoGridFSException</span><br><span class="line">[180] =&gt; MongoResultException</span><br><span class="line">[181] =&gt; MongoWriteConcernException</span><br><span class="line">[182] =&gt; MongoDuplicateKeyException</span><br><span class="line">[183] =&gt; MongoExecutionTimeoutException</span><br><span class="line">[184] =&gt; MongoProtocolException</span><br><span class="line">[185] =&gt; MongoTimestamp</span><br><span class="line">[186] =&gt; MongoInt32</span><br><span class="line">[187] =&gt; MongoInt64</span><br><span class="line">[188] =&gt; MongoLog</span><br><span class="line">[189] =&gt; MongoPool</span><br><span class="line">[190] =&gt; MongoMaxKey</span><br><span class="line">[191] =&gt; MongoMinKey</span><br></pre></td></tr></table></figure>
<p>Mongodb的函数列表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">[100] =&gt; MongoDB\Driver\Command</span><br><span class="line">   [101] =&gt; MongoDB\Driver\Cursor</span><br><span class="line">   [102] =&gt; MongoDB\Driver\CursorId</span><br><span class="line">   [103] =&gt; MongoDB\Driver\Manager</span><br><span class="line">   [104] =&gt; MongoDB\Driver\Query</span><br><span class="line">   [105] =&gt; MongoDB\Driver\ReadConcern</span><br><span class="line">   [106] =&gt; MongoDB\Driver\ReadPreference</span><br><span class="line">   [107] =&gt; MongoDB\Driver\Server</span><br><span class="line">   [108] =&gt; MongoDB\Driver\BulkWrite</span><br><span class="line">   [109] =&gt; MongoDB\Driver\WriteConcern</span><br><span class="line">   [110] =&gt; MongoDB\Driver\WriteConcernError</span><br><span class="line">   [111] =&gt; MongoDB\Driver\WriteError</span><br><span class="line">   [112] =&gt; MongoDB\Driver\WriteResult</span><br><span class="line">   [113] =&gt; MongoDB\Driver\Exception\LogicException</span><br><span class="line">   [114] =&gt; MongoDB\Driver\Exception\RuntimeException</span><br><span class="line">   [115] =&gt; MongoDB\Driver\Exception\UnexpectedValueException</span><br><span class="line">   [116] =&gt; MongoDB\Driver\Exception\InvalidArgumentException</span><br><span class="line">   [117] =&gt; MongoDB\Driver\Exception\ConnectionException</span><br><span class="line">   [118] =&gt; MongoDB\Driver\Exception\AuthenticationException</span><br><span class="line">   [119] =&gt; MongoDB\Driver\Exception\SSLConnectionException</span><br><span class="line">   [120] =&gt; MongoDB\Driver\Exception\WriteException</span><br><span class="line">   [121] =&gt; MongoDB\Driver\Exception\BulkWriteException</span><br><span class="line">   [122] =&gt; MongoDB\Driver\Exception\ExecutionTimeoutException</span><br><span class="line">   [123] =&gt; MongoDB\Driver\Exception\ConnectionTimeoutException</span><br><span class="line">   [124] =&gt; MongoDB\BSON\Binary</span><br><span class="line">   [125] =&gt; MongoDB\BSON\Javascript</span><br><span class="line">   [126] =&gt; MongoDB\BSON\MaxKey</span><br><span class="line">   [127] =&gt; MongoDB\BSON\MinKey</span><br><span class="line">   [128] =&gt; MongoDB\BSON\ObjectID</span><br><span class="line">   [129] =&gt; MongoDB\BSON\Regex</span><br><span class="line">   [130] =&gt; MongoDB\BSON\Timestamp</span><br><span class="line">   [131] =&gt; MongoDB\BSON\UTCDateTime</span><br></pre></td></tr></table></figure>
<p>所以要使用Php7.0以上版本需要重新写连接mongdo的数据库<br>
查找php文档<a href="http://php.net/manual/zh/set.mongodb.php" target="_blank" rel="noopener">Mongodb driver</a></p>
<p>修改成为mongodb的一个demo	连接修改代码如下：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">$serv = <span class="keyword">new</span> swoole_server(<span class="string">"0.0.0.0"</span>, <span class="number">9501</span>);</span><br><span class="line"></span><br><span class="line">$serv-&gt;set(<span class="keyword">array</span>(</span><br><span class="line">	<span class="string">'worker_num'</span> =&gt; <span class="number">8</span>, <span class="comment">//工作进程数量</span></span><br><span class="line">	<span class="string">'daemonize'</span> =&gt; <span class="keyword">false</span>, <span class="comment">//是否作为守护进程</span></span><br><span class="line">));</span><br><span class="line"></span><br><span class="line">$serv-&gt;on(<span class="string">'connect'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv, $fd)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">echo</span> <span class="string">"Client:Connect.\n"</span>;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$serv-&gt;on(<span class="string">'receive'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv, $fd, $from_id, $data)</span> </span>&#123;</span><br><span class="line">	</span><br><span class="line">	$arr = json_decode($data);</span><br><span class="line">	</span><br><span class="line">	$bulk = <span class="keyword">new</span> MongoDB\Driver\BulkWrite([<span class="string">'ordered'</span> =&gt; <span class="keyword">true</span>]);</span><br><span class="line">	</span><br><span class="line">	$bulk-&gt;insert($arr);</span><br><span class="line">	</span><br><span class="line">	$connection = <span class="keyword">new</span> MongoDB\Driver\Manager(<span class="string">"mongodb://localhost:27017"</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">	</span><br><span class="line">		$result = $connection-&gt;executeBulkWrite(<span class="string">'test.roomid'</span>, $bulk);</span><br><span class="line">	</span><br><span class="line">	&#125; <span class="keyword">catch</span> (MongoDB\Driver\<span class="keyword">Exception</span>\BulkWriteException $e) &#123;</span><br><span class="line">		</span><br><span class="line">			$result = $e-&gt;getWriteResult();</span><br><span class="line">	</span><br><span class="line">			<span class="keyword">if</span> ($writeConcernError = $result-&gt;getWriteConcernError()) &#123;</span><br><span class="line">				printf(<span class="string">"%s (%d): %s\n"</span>,	$writeConcernError-&gt;getMessage(),</span><br><span class="line">				$writeConcernError-&gt;getCode(),</span><br><span class="line">				var_export($writeConcernError-&gt;getInfo(), <span class="keyword">true</span>));</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="comment">// Check if any write operations did not complete at all</span></span><br><span class="line">			<span class="keyword">foreach</span> ($result-&gt;getWriteErrors() <span class="keyword">as</span> $writeError) &#123;</span><br><span class="line">				printf(<span class="string">"Operation#%d: %s (%d)\n"</span>,</span><br><span class="line">				$writeError-&gt;getIndex(),</span><br><span class="line">				$writeError-&gt;getMessage(),</span><br><span class="line">				$writeError-&gt;getCode()</span><br><span class="line">			);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125; <span class="keyword">catch</span> (MongoDB\Driver\<span class="keyword">Exception</span>\<span class="keyword">Exception</span> $e) &#123;</span><br><span class="line">	printf(<span class="string">"Other error: %s\n"</span>, $e-&gt;getMessage());</span><br><span class="line">	<span class="keyword">exit</span>;</span><br><span class="line">&#125;</span><br><span class="line">	</span><br><span class="line">	printf(<span class="string">"Inserted %d document(s)\n"</span>,$result-&gt;getInsertedCount());</span><br><span class="line">	printf(<span class="string">"Updated  %d document(s)\n"</span>,$result-&gt;getModifiedCount());</span><br><span class="line">	</span><br><span class="line">	$serv-&gt;send($fd, <span class="string">'Swoole: '</span> . $data);</span><br><span class="line">	</span><br><span class="line">	$serv-&gt;close($fd);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$serv-&gt;on(<span class="string">'close'</span>, <span class="function"><span class="keyword">function</span> <span class="params">($serv, $fd)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">echo</span> <span class="string">"Client: Close.\n"</span>;</span><br><span class="line">	</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$serv-&gt;start();</span><br></pre></td></tr></table></figure>
<p>模拟请求数据</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">netcat 127.0.0.1 9501</span><br></pre></td></tr></table></figure>
<p>END</p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://webwlsong.gitee.io/2016/05/18/mongo-php-error/" title="用swoole TCP Server 和mongodb做数据备份和恢复" target="_blank" rel="external">https://webwlsong.gitee.io/2016/05/18/mongo-php-error/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/webwlsong" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="https://static01.imgkr.com/temp/e857da9acde846d4a09582984209f398.png" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/webwlsong" target="_blank"><span class="text-dark">webwlsong</span><small class="ml-1x">Developer</small></a></h3>
        <div>个人简介。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/2016/11/25/2016-11-26/" title="解决go delve 不能使用 OS X cannot get thread count"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/2016/04/28/go-sty01/" title="go基本语法"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>赏</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>感谢您的支持，我会继续努力的!</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="https://webwlsong-1301722012.cos.ap-shanghai.myqcloud.com/mweb/2020/08/29/alipay.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开支付宝扫一扫，即可进行扫码打赏哦</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="https://webwlsong-1301722012.cos.ap-shanghai.myqcloud.com/mweb/2020/08/29/wx_pay.jpg" alt="扫码支持" title="扫一扫" />
              </div>
              <p class="text-muted mv">扫码打赏，你说多少就多少</p>
              <p class="text-grey">打开微信扫一扫，即可进行扫码打赏哦</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> 支付宝</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> 微信支付</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/webwlsong" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://weibo.com/webwlsong" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="https://twitter.com/webwlsong" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
        
        <li><a href="https://www.behance.net/webwlsong" target="_blank" title="Behance" data-toggle=tooltip data-placement=top><i class="icon icon-behance"></i></a></li>
        
        <li><a href="/atom.xml" target="_blank" title="Rss" data-toggle=tooltip data-placement=top><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/js/plugin.min.js"></script>
<script src="/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>





   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: 'ectr7l88yk7atjrzq1h0ptl10z5lck7psw1bvm0hxfio896h',
    appKey: 'w1vhw2prhcgtuxero9gyhb0ie6apgyj6b1kv0e8rbkeg19jp',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: false
  });
  </script>

     







</body>
</html>